package cn.stylefeng.guns.modular.base.item.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.base.item.entity.ItemClass;
import cn.stylefeng.guns.modular.base.item.enums.ItemClassExceptionEnum;
import cn.stylefeng.guns.modular.base.item.mapper.ItemClassMapper;
import cn.stylefeng.guns.modular.base.item.pojo.request.ItemClassRequest;
import cn.stylefeng.guns.modular.base.item.service.ItemClassService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 基础数据-存货分类业务实现层
 *
 * @author caoxf
 * @since 2023/11/20 22:56
 */
@Service
public class ItemClassServiceImpl extends ServiceImpl<ItemClassMapper, ItemClass> implements ItemClassService {

	@Override
    public void add(ItemClassRequest itemclassRequest) {
        ItemClass itemclass = new ItemClass();
        BeanUtil.copyProperties(itemclassRequest, itemclass);
        this.save(itemclass);
    }

    @Override
    public void del(ItemClassRequest itemclassRequest) {
        ItemClass itemclass = this.queryItemclass(itemclassRequest);
        this.removeById(itemclass.getClassCode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(ItemClassRequest itemclassRequest) {
        this.removeByIds(itemclassRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(ItemClassRequest itemclassRequest) {
        ItemClass itemclass = this.queryItemclass(itemclassRequest);
        BeanUtil.copyProperties(itemclassRequest, itemclass);
        this.updateById(itemclass);
    }

    @Override
    public ItemClass detail(ItemClassRequest itemclassRequest) {
        return this.queryItemclass(itemclassRequest);
    }

    @Override
    public PageResult<ItemClass> findPage(ItemClassRequest itemclassRequest) {
        LambdaQueryWrapper<ItemClass> wrapper = createWrapper(itemclassRequest);
        Page<ItemClass> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<ItemClass> findList(ItemClassRequest itemclassRequest) {
        LambdaQueryWrapper<ItemClass> wrapper = this.createWrapper(itemclassRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author caoxf
     * @since 2023/11/20 22:56
     */
    private ItemClass queryItemclass(ItemClassRequest itemclassRequest) {
        ItemClass itemclass = this.getById(itemclassRequest.getClassCode());
        if (ObjectUtil.isEmpty(itemclass)) {
            throw new ServiceException(ItemClassExceptionEnum.ITEMCLASS_NOT_EXISTED);
        }
        return itemclass;
    }

    /**
     * 创建查询wrapper
     *
     * @author caoxf
     * @since 2023/11/20 22:56
     */
    private LambdaQueryWrapper<ItemClass> createWrapper(ItemClassRequest itemclassRequest) {
        LambdaQueryWrapper<ItemClass> queryWrapper = new LambdaQueryWrapper<>();

        String classCode = itemclassRequest.getClassCode();
        String className = itemclassRequest.getClassName();

        queryWrapper.like(ObjectUtil.isNotEmpty(classCode), ItemClass::getClassCode, classCode);
        queryWrapper.like(ObjectUtil.isNotEmpty(className), ItemClass::getClassName, className);

        return queryWrapper;
    }

}
